package com.zdsoft.blp.zongbuju.shouye.bianmin.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.bianmin.adapter.BMsqhdAdapter;
import com.zdsoft.blp.zongbuju.shouye.bianmin.entity.SheQu;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/30.
 */
public class BMsqhdActivity extends BaseActivity implements XListView.IXListViewListener, NetCallback {
    private XListView xListView;
    private TextView tv_title;
    private List<SheQu> list;
    private List<SheQu> mlist;
    private SheQu sheQu;
    private int pageNo = 1;
    private BMsqhdAdapter adapter;

    @Override
    protected void init() {
        setContentView(R.layout.bianmin_sqhd);
        xListView = find(R.id.xlv_sqhd);
        tv_title = find(R.id.tv_title);
        tv_title.setText("社区活动");
        mlist = new ArrayList<>();
        xListView.setXListViewListener(this);
        xListView.setPullRefreshEnable(true);//设置可以刷新
        xListView.setPullLoadEnable(true);//设置可以加载更多
        showMoreData();


    }

    private void showMoreData() {
        Net net = new Net();
        net.url = API.GENERALINFO_URL;
        net.nameSpace = API.GENERALINFO_NAMESPACE;
        net.methodName = API.GENERALINFOLIST;
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 7);
        map.put("columnCode", "sqdt");
        map.put("communityCode",communityCode);
        map.put("condition", "");
        map.put("curTime", "");
        net.parms = map;
        getServer(net, this);
    }


    //下拉刷新
    @Override
    public void onRefresh() {
        mlist = new ArrayList<>();
        pageNo = 1;
        showMoreData();
    }

    //上拉加载更多
    @Override
    public void onLoadMore() {
        pageNo++;
        showMoreData();
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("res", res);
        if (flag) {
            if (pageNo == 1) {
                mlist = jiexi(res);
                adapter = new BMsqhdAdapter(this, mlist);
                xListView.setAdapter(adapter);
                xListView.stopRefresh();
            } else {
                list = jiexi(res);
                for (SheQu sheQus : list) {
                    mlist.add(sheQus);
                }
                adapter.setListData(mlist);
                xListView.stopLoadMore();
            }
            //xListView添加数据源
            xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(BMsqhdActivity.this, BMxqActivity.class);
                    intent.putExtra("id", mlist.get(position - 1).getId());
                    startActivity(intent);
                }
            });
        }

    }

    private List<SheQu> jiexi(String res) {
        list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(res);
            String total = object.getString("total");
            String pageNo = object.getString("pageNo");
            String pageSize = object.getString("pageSize");
            JSONArray array = object.getJSONArray("rows");
            for (int i = 0; i < array.length(); i++) {
                JSONObject object2 = array.getJSONObject(i);
                String id = object2.getString("id");
                String picPath = object2.getString("picPath");
                String content = object2.getString("content");
                String title = object2.getString("title");
                String releaseDate = object2.getString("releaseDate");
                sheQu = new SheQu(id, title, content, picPath, releaseDate);
                list.add(sheQu);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}
